package com.hatic.purchase;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.key.KeyCreator;

/**
 * 投标单位事件处理
 * @author caojiaxin
 * @version 1.0,2011-10-18
 */
public class PurProviderBO extends BusinessObject{

	private static final long serialVersionUID = 1L;

	/**
	 * 自动新增标书发售记录,修改投标单位明细'是否发售标书'记录值为“是”
	 * @param request
	 * @return
	 */
	public String onFileSale(String[] purproviIds,String filesale_user,String add_userid,String filesale_dept){
		
		if(purproviIds == null || purproviIds.length == 0){
			setMessage("请选择一条记录！");
			return _returnFaild;
		}
		for (int i = 0; i < purproviIds.length; i++) {
			if(!createFileSale(purproviIds[i], filesale_user, filesale_dept, add_userid)){
				setMessage("新增标书发售记录出错！");
				return _returnFaild;
			}
			if(!updatePurProvider(purproviIds[i])){
				setMessage("修改投标单位明细出错！");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	/**
	 * 评标报告保存后，向投标单位排名表中导入供应商
	 * @param recordId
	 * @return
	 */
	public String onImproviders(String recordId){
		if(recordId == null || recordId.equals("")){
			_log.showDebug("取不到功能主键！");
			return _returnFaild;
		}
		if(!insertProviders(recordId)){
			setMessage("导入供应商明细出现错误！");
			return _returnFaild;
		}
		
		return _returnSuccess;
	}
	
	/**
	 * 向投标单位排名表中导入供应商
	 * @param recordId
	 * @return
	 */
	private boolean insertProviders(String auditId) {
		String sql="select audit_id,apply_id from pur_apply_audit where audit_id = ? ";
		
		DaoParam qparam=_dao.createParam(sql);
		qparam.addStringValue(auditId);
		Map<String, String> map=_dao.queryMap(qparam);
		_log.showDebug("===========audit_id="+auditId+";execute sql = "+sql);
		
		String sql2="update pur_provider set audit_id = ? where apply_id = ? ";
		
		
		String audit_id=map.get("audit_id");
		String apply_id=map.get("apply_id");
	
		DaoParam iparam=_dao.createParam(sql2);
		iparam.addStringValue(audit_id);
		iparam.addStringValue(apply_id);
		
		_log.showDebug("===========execute sql = "+sql2);
		boolean res=_dao.update(iparam);
		if(!res) return false;
		
		
		return true;
	}
	/**
	 * 新增标书发售记录
	 * @param proviId
	 * @param filesale_user
	 * @param filesale_date
	 * @param filesale_dept
	 * @param add_userid
	 * @return
	 */
	private boolean createFileSale(String proviId,String filesale_user,String filesale_dept,String add_userid){
		String querySql="select * from pur_provider where pur_provid = ?";
		DaoParam dp=_dao.createParam(querySql);
		dp.addStringValue(proviId);
		Map<String, String> proviMap=_dao.queryMap(dp);
		
		String createSql="insert into pur_apply_filesale(filesale_id,apply_id,provider_id,filesale_user,filesale_phone,filesale_date,"+
		" filesale_dept,rece_dept,rece_user,rece_phone,file_desc,add_userid,add_date) "+
		" values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
		_log.showDebug("==============proviId='"+proviId+"';sql="+createSql);
		
		String filesaleId=KeyCreator.getInstance().createKey("pur_apply_filesale");
		
		DaoParam param = _dao.createParam(createSql);
		param.addStringValue(filesaleId);
		param.addStringValue(proviMap.get("apply_id"));
		param.addStringValue(proviMap.get("provider_id"));
		param.addStringValue(filesale_user);
		param.addStringValue("");
		param.addDateValue(DateUtil.getToday());
		param.addStringValue(filesale_dept);
		param.addStringValue(proviMap.get("provider_name"));
		param.addStringValue(proviMap.get("ywdb"));
		param.addStringValue(proviMap.get("businessp"));
		param.addDateValue("");
		param.addStringValue(add_userid);
		param.addDateValue(DateUtil.getToday());
		
		return _dao.update(param);
	}
	
	/**
	 * 修改投标单位明细 --是否发售标书为“是”
	 * @param proviId
	 * @return
	 */
	private boolean updatePurProvider(String proviId){
		String sql="update pur_provider set isfilesale = '1' where pur_provid = ?";
		
		DaoParam param=_dao.createParam(sql);
		param.addStringValue(proviId);
		
		return _dao.update(param);
	}
	
}
